#define _CRT_SECURE_NO_WARNINGS  1


#include<iostream>
#include<vector>
#include<string>
#include<unordered_map>
using namespace std;

class Solution
{
public:
    vector<int> findAnagrams(string s, string p)
    {
        vector<int> v;
        unordered_map<char, int> m1;
        unordered_map<char, int> m2;
        int count = 0;
        for (int i = 0; i < p.length(); i++)
        {
            m2[p[i]]++;
        }
        int left = 0;
        int right = 0;
        while (right < s.length())
        {
            while (right - left < p.length())
            {
                if (m1[s[right]] < m2[s[right]])
                    count++;
                m1[s[right]]++;
                right++;
            }
            if (count == p.length())
                v.push_back(left);
            if (m1[s[left]] <= m2[s[left]])
                count--;
            m1[s[left]]--;
            left++;
            m1[s[right]]++;
            if (m1[s[right]] <= m2[s[right]])
                count++;
            right++;
        }
        return v;
    }
};

int main()
{
    string s1{ 'c','b','a','e','b','a','b','a','c','d' };
    string s2{ 'a','b','c' };
    Solution m;
    vector<int> v = m.findAnagrams(s1, s2);
    for (int i = 0; i < v.size(); i++)
    {
        cout << v[i] << endl;
    }
    return 0;
}